package com.example.service;

import com.example.entity.Klass;
import com.example.entity.Student;
import com.example.entity.Teacher;
import com.example.mapper.KlassMapper;
import com.example.mapper.StudentMapper;
import com.example.mapper.TeacherMapper;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryCondition;
import xyz.lwm.lazycat.annotation.Inject;
import xyz.lwm.lazycat.transaction.Transactional;
import xyz.lwm.lazycat.transaction.execute.Propagation;
import xyz.lwm.lazycat.utility.Assert;
import xyz.lwm.lazycat.web.annocation.Service;

import javax.annotation.Resource;

@Service
public class AopService {

    @Inject
    private KlassMapper klassMapper;

    @Resource
    private StudentMapper studentMapper;

    @Inject
    private TeacherMapper teacherMapper;

    @Inject
    private AopService me;

    @Transactional
    public void deleteAllSuccessfully() {
        klassMapper.updateByCondition(new Klass().setIsDel(1), QueryCondition.create(new QueryColumn("id"), ">", 0));
        studentMapper.updateByCondition(new Student().setIsDel(1), QueryCondition.create(new QueryColumn("id"), ">", 0));
//        Assert.isTrue(false, "删除数据异常！");
        deleteTeacher();
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NESTED)
    public void deleteTeacher() {
        teacherMapper.updateByCondition(new Teacher().setIsDel(1), QueryCondition.create(new QueryColumn("id"), ">", 0));
        Assert.isTrue(false, "删除数据异常！");
    }

    @Transactional
    public void resumeAllSuccessfully() {
        klassMapper.updateByCondition(new Klass().setIsDel(0), QueryCondition.create(new QueryColumn("id"), ">", 0));
        studentMapper.updateByCondition(new Student().setIsDel(0), QueryCondition.create(new QueryColumn("id"), ">", 0));
        teacherMapper.updateByCondition(new Teacher().setIsDel(0), QueryCondition.create(new QueryColumn("id"), ">", 0));
    }

}
